﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Utilities.Collections;
using Smart_Archives.Entity;
using Smart_Archives.Models.Enum;
using Smart_Archives.Utils;

namespace Smart_Archives.Controllers
{
    /// <summary>
    /// 设备分组
    /// </summary>
    public class DeviceCategoryController : Controller
    {
        private readonly SaContext _saContext;

        public DeviceCategoryController(SaContext saContext)
        {
            _saContext = saContext;
        }

        public ActionResult Index()
        {
            var rootCategories = _saContext.SecurityDeviceCategory
                .Where(a => a.ParentId == 0 && a.Status == (int)StatusEnum.Use).ToList();
            ViewBag.rootCategories = rootCategories;
            var parentCategories = _saContext.SecurityDeviceCategory
                .Where(a => a.ParentId != 0 && a.Status == (int)StatusEnum.Use).ToList();
            return View(parentCategories);
        }

        /// <summary>
        /// 删除分组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CategoryDelete()
        {
            try
            {
                int id = Request.Form["id"].ToInt();
                var model = _saContext.SecurityDeviceCategory.Find(id);
                model.Status = 0;
                _saContext.SaveChanges();
                return Json(new { succeed = true, message = "删除成功" });
            }
            catch (Exception e)
            {
                return Json(new { succeed = false, message = "删除失败" });
            }

        }

        /// <summary>
        /// 分组编辑
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CategoryEdit()
        {
            int id = Request.Query["id"].ToInt();
            var model = new SecurityDeviceCategory();
            if (id > 0)
            {
                model = _saContext.SecurityDeviceCategory.Find(id);
            }
            var rootCategories = _saContext.SecurityDeviceCategory
                .Where(a => a.ParentId == 0 && a.Status == (int)StatusEnum.Use).ToList();
            ViewBag.rootCategories = rootCategories;
            return View(model);
        }

        /// <summary>
        /// 分组编辑处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CategoryEditProcess()
        {
            try
            {
                int id = Request.Form["id"].ToInt();
                int rootId = Request.Form["rootId"].ToInt();
                var name = Request.Form["name"];
                var model = new SecurityDeviceCategory();
                if (id > 0)
                {
                    model = _saContext.SecurityDeviceCategory.Find(id);
                }
                model.UpdateTime = DateTime.Now;
                model.ParentId = rootId;
                model.Name = name;
                model.Type = _saContext.SecurityDeviceCategory.Find(rootId).Type;
                model.CreateTime = DateTime.Now;
                model.Status = (int)StatusEnum.Use;
                //判断是新增还是修改
                if (id <= 0)
                {
                    _saContext.SecurityDeviceCategory.Add(model);
                }

                _saContext.SaveChanges();
                return Json(new { succeed = true });
            }
            catch (Exception e)
            {
                return Json(new { succeed = false });
            }
        }

        /// <summary>
        /// 设备列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult DeviceIndex()
        {
            var pageIndex = Request.Query["page"].ToInt(1);
            var keyword = Request.Query["keyword"];
            var selectType = Request.Query["selectType"].ToInt(-1);
            int pageSize = 10;

            var items = _saContext.SecurityDevice.Where(a => a.Status == (int)StatusEnum.Use);

            if (!string.IsNullOrEmpty(keyword) || selectType >= 0)
            {
                //搜索条件判断
                if (!string.IsNullOrEmpty(keyword))
                    items = _saContext.SecurityDevice.Where(a => a.Name.Contains(keyword));
                if (selectType >= 0)
                    items = _saContext.SecurityDevice.Where(a => a.DeviceType == selectType);
            }

            var itemCount = items.Count();
            var pageCount = (int)Math.Ceiling(itemCount / (double)pageSize);

            var list = items.OrderByDescending(a => a.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var rootCategories = _saContext.SecurityDeviceCategory
                .Where(a => a.ParentId == 0 && a.Status == (int)StatusEnum.Use).ToList();
            ViewBag.rootCategories = rootCategories;
            ViewBag.itemCount = itemCount;
            ViewBag.pageCount = pageCount;

            return View(list);
        }
        /// <summary>
        /// 设备详情页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DeviceDetail()
        {
            var id = Request.Query["id"].ToInt();
            var model = _saContext.SecurityDevice.Find(id);
            ViewBag.categoryName = _saContext.SecurityDeviceCategory.Find(model.SecurityCategoryId).Name;
            return View(model);
        }

        /// <summary>
        /// 设备编辑页
        /// </summary>
        /// <returns></returns>
        public ActionResult DeviceEdit()
        {
            var id = Request.Query["id"].ToInt();
            var model = _saContext.SecurityDevice.Find(id);
            var category = _saContext.SecurityDeviceCategory
                .Where(a => a.Type == model.DeviceType && a.Status == (int)StatusEnum.Use && a.ParentId != 0).ToList();
            ViewBag.category = category;
            return View(model);
        }

        /// <summary>
        /// 设备编辑处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeviceEditProcess()
        {
            var name = Request.Form["name"];
            var categoryId = Request.Form["categoryId"].ToInt();
            var id = Request.Form["id"].ToInt();
            var model = _saContext.SecurityDevice.Find(id);
            model.SecurityCategoryId = categoryId;
            model.Name = name;
            _saContext.SaveChanges();
            return Json(new { succeed = true });
        }
    }
}